# Author: Mr.Xu
# @Time : 2019/12/3 21:57

from .celery import app
# 获取项目中的模型类
from home.models import Banner
from django.conf import settings
from django.core.cache import cache
from home.serializers import BannerModelSerializer
@app.task
def update_banner_cache():
    # 查询数据库
    banner_query = Banner.objects.filter(is_delete=False, is_show=True).order_by("-order")[:settings.BANNER_COUNT]
    # 序列化数据对象得到序列化后的json列表
    banner_list = BannerModelSerializer(banner_query, many=True).data
    # 设置缓存,走的是默认项目配置的缓存数据库
    cache.set('banner_list', banner_list)


from utils.response import APIResponse
from libs.tx_sms import send_sms

# 需要手机号、验证码、过期时间(min)
@app.task
def send_sms_task(mobile, code, exp):
    result = send_sms(mobile, code, exp)
    if not result:
        return APIResponse(1, "验证码发送失败")